package com.infotel.chat;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class TestChatXML {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();

            SAXParser parser = factory.newSAXParser();

            ArrayList<Users> users = new ArrayList<>();
            ArrayList<Messages> messages = new ArrayList<>();

            DefaultHandler gestionnaireSAX = new GestionChat(users, messages);
            parser.parse("src/com/infotel/chat/chat.xml", gestionnaireSAX);
            
            JOptionPane.showMessageDialog(null, users.size()+":"+messages.size());
            
            for(Users u : users){
                System.out.println(u+"\n");
            }
            for(Messages m : messages){
                System.out.println(m+"\n");
            }
        } catch (ParserConfigurationException | SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

class GestionChat extends DefaultHandler {
    private ArrayList<Users> users;
    private ArrayList<Messages> messages;
    char[] buffer =null;
    int count=0;
    public GestionChat(ArrayList<Users> users, ArrayList<Messages> messages) {
        this.users = users;
        this.messages = messages;
    }

    @Override
    public void startElement(String uriEspaceNom, String nomElementSansPrefixe,
            String nomElement, Attributes attributs) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        if ("user".equals(nomElement)) {
            users.add(new Users(attributs.getValue("id"), attributs
                    .getValue("email"), attributs.getValue("gender")));
        }else{
            if ("message".equals(nomElement)) {
                try {
                    messages.add(new Messages(attributs.getValue("author"), sdf.parse(attributs
                            .getValue("date")),""));
                    
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    }
    
    @Override
    public void endElement(String uriEspaceNom, String nomElementSansPrefixe,
            String nomElement){
        System.out.println("plop");
    }
    
    
    @Override 
    public void characters(char[] buffer, int start, int length){
        count=start;
        this.buffer=buffer;
        if(messages.size()>0){
            String content = "";
            for(int i=start;i<buffer.length;i++){
                content+=buffer[i];
            }
           messages.get(messages.size()-1).setContent(content);
        }
    }
}
